package org.slusk.thynwor.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import javax.swing.JButton;

import org.slusk.thynwor.corestructures.Drone;
import org.slusk.thynwor.corestructures.Environment;
import org.slusk.thynwor.corestructures.GridMap;

import java.awt.Canvas;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.LinkedList;

import javax.swing.JToggleButton;

public class ThynworGraphics implements IThynworWindow {

	private Environment environment;
	private JFrame frame;
	private JToggleButton tglbtnRunSimulation;
	private JButton btnReset;
	private Surface surface;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ThynworGraphics window = new ThynworGraphics();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ThynworGraphics() {
		environment = new Environment();
		initialize();
	}

	public ThynworGraphics(Environment env) {
		environment = env;
		initialize();
		this.getFrame().setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 550, 550);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		frame.getContentPane().add(toolBar, BorderLayout.NORTH);
		
		tglbtnRunSimulation = new JToggleButton("Run Simulation");
		tglbtnRunSimulation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				toggleRunSim();
			}
		});
		toolBar.add(tglbtnRunSimulation);
		
		JButton btnChangeChromosome = new JButton("Change Chromosome");
		btnChangeChromosome.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		toolBar.add(btnChangeChromosome);
		
		btnReset = new JButton("Reset");
		toolBar.add(btnReset);
		
		surface = new Surface();
		frame.getContentPane().add(surface, BorderLayout.CENTER);
	}

//	public Canvas getCanvas() {
//		return canvas;
//	}
	
	private void toggleRunSim() {
		if (tglbtnRunSimulation.isSelected()) {
			environment.startSim();
		} else {
			environment.stopSim();
		}
	}
	public JToggleButton getTglbtnRunSimulation() {
		return tglbtnRunSimulation;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setMap(GridMap map) {
//		Graphics2D g = (Graphics2D) surface.getGraphics();
//		g.setBackground(new Color(0,0,0));
//		g.setColor(new Color(255,255,255));
	}

	@Override
	public void update() {
		surface.repaint();
	}
	
	public JFrame getFrame() {
		return frame;
	}
	
	/*
	 * 
	 */
	class Surface extends JPanel {

		public Surface() {
			setBackground(new Color(0,0,0));
			setForeground(new Color(255,255,255));
		}
		
	    private void doDrawing(Graphics g) {

	        Graphics2D g2d = (Graphics2D) g;
//	        g2d.drawString("Java 2D", 50, 50);
//
//			int x = (int) Math.round(Math.random() * 2 - 1);
//			int y = (int) Math.round(Math.random() * 2 - 1);
//			g.drawString("check", 10+x, 10+y);
	        

	        Dimension size = getSize();
	        Insets ins = getInsets();
	        
//	        System.out.println("top:"+ins.top+"; bottom:"+ins.bottom+"; left:"+ins.left+"; right:"+ins.right);
//	        System.out.println("height:"+ size.height+ "; width:"+size.width);
	        LinkedList<Drone> droneList = environment.getDroneList();
	        for (Drone drone : droneList) {
	        	g2d.drawString("*", drone.getCoordinates().x*30, drone.getCoordinates().y*30);
	        }      
	    }

	    @Override
	    public void paintComponent(Graphics g) {

	        super.paintComponent(g);
	        
	        doDrawing(g);
	    }
	}
}
